package com.hxkj.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxkj.dao.FriendlyLinkDao;
import com.hxkj.domain.enums.FriendlyLinkExceptionEnum;
import com.hxkj.domain.po.FriendlyLink;
import com.hxkj.service.FriendlyLinkService;
import com.hxkj.utils.StrSliptUtils;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 友情链接管理实现类
 * <p>
 * Created by chenfangzhou on 2018/5/15.
 */
@Service
@Slf4j
public class FriendlyLinkServiceImpl implements FriendlyLinkService {

    @Autowired
    private FriendlyLinkDao friendlyLinkDao;

    @Override
    public List<FriendlyLink> getFriendlyLinkListNoPage(FriendlyLink friendlyLink) throws Exception {
        friendlyLink.setDelTag(0);
        friendlyLink.setIsEnable((byte) 0);
        return friendlyLinkDao.get(friendlyLink);
    }

    /**
     * 保存友情链接
     *
     * @param friendlyLink
     * @return
     * @throws Exception
     */
    @Override
    public ResultMessageStruct saveFriendlyLink(FriendlyLink friendlyLink) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        // 根据链接判断是否重复
        FriendlyLink link = new FriendlyLink();
        link.setLinkUrl(friendlyLink.getLinkUrl());
        link.setDelTag(0);
        link.setSiteId(friendlyLink.getSiteId());
        try {
            if (friendlyLinkDao.get(link).size() > 0) {
                resultMessageStruct.setCode(FriendlyLinkExceptionEnum.FRIENDLYLINK_EXIST_ALREADY.getCode());
            } else {
                friendlyLink.setCreateTime(System.currentTimeMillis());
                friendlyLink.setUpdateTime(System.currentTimeMillis());
                int i = friendlyLinkDao.save(friendlyLink);
                resultMessageStruct.setCode(200);
            }
            return resultMessageStruct;
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(FriendlyLinkExceptionEnum.FRIENDLYLINK_SAVE_ERROR.getCode());
        }
        return resultMessageStruct;
    }

    /**
     * 获取单条友情链接
     *
     * @param friendlyLink
     * @return
     * @throws Exception
     */
    @Override
    public ResultDataStruct<FriendlyLink> getFriendlyLink(FriendlyLink friendlyLink) throws Exception {

        ResultDataStruct<FriendlyLink> resultDataStruct = new ResultDataStruct<>();
        try {
            FriendlyLink link = friendlyLinkDao.get(friendlyLink.getLinkId());

            if (link == null) {
                resultDataStruct.setCode(FriendlyLinkExceptionEnum.FRIENDLYLINK_IS_NULL.getCode());
                return resultDataStruct;
            } else {
                if (link.getDelTag() == 1) {
                    resultDataStruct.setCode(FriendlyLinkExceptionEnum.FRIENDLYLINK_IS_NULL.getCode());
                    return resultDataStruct;
                } else {
                    resultDataStruct.setCode(200);
                    resultDataStruct.setData(link);
                    return resultDataStruct;
                }
            }
        } catch (Exception e) {
            resultDataStruct.setCode(FriendlyLinkExceptionEnum.FRIENDLYLINK_QUERY_ERROR.getCode());
            e.printStackTrace();
            return resultDataStruct;
        }
    }


    /**
     * 获取友情链接列表,带分页
     *
     * @param friendlyLink
     * @param pageParam
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<FriendlyLink> getFriendlyLinkList(FriendlyLink friendlyLink, PageTo pageParam) throws Exception {
        // 前端没有传分页参数,默认设置第一页,取10条
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10000);
        }
        try {
            Page<FriendlyLink> page = PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            friendlyLink.setDelTag(0);
            friendlyLinkDao.get(friendlyLink);
            PageInfo<FriendlyLink> pageInfo = new PageInfo<>(page);
            return pageInfo;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 修改友情链接
     *
     * @param friendlyLink
     * @return
     */
    @Override
    public ResultMessageStruct updateFriendlyLink(FriendlyLink friendlyLink) {
        ResultMessageStruct result = new ResultMessageStruct();
        try {
            if (StringUtils.isNoneBlank(friendlyLink.getLinkUrl())) {
                // 判断链接地址是否已经存在，如果存在则不允许重复添加
                FriendlyLink linkUrl = friendlyLinkDao.selectLinkUrl(friendlyLink);
                if (linkUrl != null) {
                    result.setCode(FriendlyLinkExceptionEnum.FRIENDLYLINK_EXIST_ALREADY.getCode());
                    return result;
                }
            }

            // 更新友情链接
            friendlyLink.setUpdateTime(System.currentTimeMillis());
            int i = friendlyLinkDao.update(friendlyLink);
            result.setCode(i > 0 ? 200 : FriendlyLinkExceptionEnum.FRIENDLYLINK_UPDATE_ERROR.getCode());
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(FriendlyLinkExceptionEnum.FRIENDLYLINK_UPDATE_ERROR.getCode());
            return result;
        }
    }


    /**
     * 删除友情链接,可批量删除,也可单条删除
     *
     * @param friendlyLink
     * @return
     */
    @Override
    public ResultMessageStruct deleteFriendlyLink(FriendlyLink friendlyLink) {
        ResultMessageStruct resultMessageStruct = new ResultDataStruct<>();
        try {
            // 删除多条
            if (friendlyLink.getLinkIds() != null && friendlyLink.getLinkIds() != "") {
                String[] str = StrSliptUtils.sliptArray(friendlyLink.getLinkIds());
                for (int i = 0; i < str.length; i++) {
                    friendlyLinkDao.delete(Long.valueOf(str[i]));
                }
            } else {// 删除单条
                friendlyLinkDao.delete(friendlyLink.getLinkId());

            }
            resultMessageStruct.setCode(200);
            return resultMessageStruct;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            resultMessageStruct.setCode(FriendlyLinkExceptionEnum.FRIENDLYLINK_DELETE_ERROR.getCode());
            return resultMessageStruct;
        }
    }
}
